<html>
<head>
  <title>Evernote Export</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="YXBJ Windows/601568 (zh-CN, DDL); Windows/10.0.0 (Win64); EDAMVersion=V2;"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="540"/>
<h1>03-循环</h1>
<div>
<table bgcolor="#D4DDE5" border="0">
<tr><td><b>创建时间：</b></td><td><i>2018/4/3 21:42</i></td></tr>
<tr><td><b>更新时间：</b></td><td><i>2020/3/29 12:33</i></td></tr>
<tr><td><b>作者：</b></td><td><i>18081003141@163.com</i></td></tr>
</table>
</div>
<br/>

<div>
<span><div><div><span style="font-size: 16pt;">计算机真的智能吗？</span></div><div><br clear="none"/></div><div><img src="00-笔记_files/第一台计算机.jpg" type="image/jpeg" data-filename="第一台计算机.jpg" style="font-size: 16pt;" width="304"/><span style="font-size: 16pt;">   <img src="00-笔记_files/大脑袋.jpg" type="image/jpeg" data-filename="大脑袋.jpg" style="font-size: 16pt;" width="274"/></span></div><div><img src="00-笔记_files/网吧.jpg" type="image/jpeg" data-filename="网吧.jpg" style="font-size: 16pt;" width="300"/>     <img src="00-笔记_files/surface.jpg" type="image/jpeg" data-filename="surface.jpg" width="344"/></div><div><br clear="none"/></div><div><img src="00-笔记_files/大数据.png" type="image/png" data-filename="大数据.png" style="font-size: 16pt;" width="301"/><span style="font-size: 16pt;">   </span><img src="00-笔记_files/AI.jpg" type="image/jpeg" data-filename="AI.jpg" style="font-size: 16pt;" width="261"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 16pt;"><span style="font-size: 16pt; color: rgb(54, 101, 238); font-weight: bold;">计算机其实很笨！所谓的智能都是人工赋予的</span></span></div><div><br clear="none"/></div><div><span style="font-size: 16pt;">讲个故事：</span></div><div><br clear="none"/></div><div><span style="font-size: 16pt;">很久很久以前，有一个熊哥</span></div><div><br clear="none"/></div><div><span style="font-size: 21.3333px;">一天，他家的水龙头坏了</span></div><div><br clear="none"/></div><div><span style="font-size: 21.3333px;">可是熊老师很好强，不愿找他人协助，所以自己买了新的水龙头回来换</span></div><div><br clear="none"/></div><div><span style="font-size: 21.3333px;">那么，问题来了，怎么换呢？</span></div><div><br clear="none"/></div><div><span style="font-size: 21.3333px;">首先需要把坏掉的水龙头拧下来</span></div><div><br clear="none"/></div><div><span style="font-size: 21.3333px;">需要拧多少下？</span></div><div><br clear="none"/></div><div><span style="font-size: 21.3333px;">只要水龙头没掉下来就一下一下的拧，直达掉下来为止</span></div><div><br clear="none"/></div><div><span style="font-size: 21.3333px;">然后，再把新的水龙头拧上去</span></div><div><br clear="none"/></div><div><span style="font-size: 21.3333px;">需要拧多少下？</span></div><div><br clear="none"/></div><div><span style="font-size: 21.3333px;">只要水龙头没拧紧就一下一下的拧，直到拧紧了为止</span>                </div><div>                                   <img src="00-笔记_files/修水龙头.jpg" type="image/jpeg" data-filename="修水龙头.jpg" style="font-size: 16pt;"/></div><div><span style="font-size: 12pt; color: rgb(54, 101, 238);">一下一下拧水龙头就是重复的操作，重复的操作就是循环！</span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 12pt;">document.write(&quot;Hello world&quot;);</span></div><div><span style="font-size: 12pt;">document.write(&quot;Hello world&quot;);</span></div><div><br clear="none"/></div><div><span style="font-size: 12pt;">... 写十遍</span></div><div><br clear="none"/></div><div><span style="font-size: 12pt;">var str = &quot;Hello world&quot;;</span></div><div><span style="font-size: 12pt;">document.write(str);</span></div><div><span style="font-size: 12pt;">document.write(str);</span></div><div><span style="font-size: 12pt;">...</span></div><div><span style="font-size: 16px;">仍然要写10遍</span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 21.3333px;">while循环</span></div><div><span style="font-size: 8pt;"> </span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><span style="font-size: 12pt; min-height: 26pt; font-family: consolas; color: #577909;">while</span><span style="font-size: 12pt; min-height: 26pt; font-family: consolas; color: #080808;">(条件){</span></div><div style="min-height: 26pt;"><span style="font-size: 12pt;"><span style="min-height: 26pt; font-family: consolas; color: #080808;">   </span> <span style="min-height: 26pt; font-family: consolas; color: #95a3ab;">//条件成立就会反复执行这里的代码</span></span></div><div style="min-height: 26pt;"><span style="min-height: 26pt; font-family: consolas; color: #080808;"><span style="font-size: 12pt;">}</span></span></div><div style="min-height: 26pt;"><span style="font-size: 12pt;"><span style="min-height: 26pt; font-family: consolas; color: #364bc0;">var</span> <span style="min-height: 26pt; font-family: consolas; color: #080808;">count</span> <span style="min-height: 26pt; font-family: consolas; color: #577909;">=</span> <span style="min-height: 26pt; font-family: consolas; color: #9b1ceb;">0</span><span style="min-height: 26pt; font-family: consolas; color: #080808;">;</span></span></div><div style="min-height: 26pt;"><span style="font-size: 12pt;"><span style="min-height: 26pt; font-family: consolas; color: #577909;">while</span><span style="min-height: 26pt; font-family: consolas; color: #080808;">(count</span> <span style="min-height: 26pt; font-family: consolas; color: #577909;">&lt;</span> <span style="min-height: 26pt; font-family: consolas; color: #9b1ceb;">10</span><span style="min-height: 26pt; font-family: consolas; color: #080808;">){</span></span></div><div style="min-height: 26pt;"><span style="font-size: 12pt;"><span style="min-height: 26pt; font-family: consolas; color: #080808;">    count</span><span style="min-height: 26pt; font-family: consolas; color: #577909;">++</span><span style="min-height: 26pt; font-family: consolas; color: #080808;">;</span></span></div><div style="min-height: 26pt;"><span style="font-size: 12pt;"><span style="min-height: 26pt; font-family: consolas; color: #080808;">   </span> <span style="min-height: 26pt; font-family: consolas; color: #3c7a03;">document</span><span style="min-height: 26pt; font-family: consolas; color: #080808;">.write(</span><span style="min-height: 26pt; font-family: consolas; color: #248c85;">&quot;Hello world&quot;</span><span style="min-height: 26pt; font-family: consolas; color: #080808;">);</span></span></div><div style="min-height: 26pt;"><span style="min-height: 26pt; font-family: consolas; color: #080808;"><span style="font-size: 12pt;">}</span></span></div></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br/></div><div><span style="font-size: 12pt; color: rgb(45, 79, 201);">死循环： 没有终止条件的循环即为死循环，在代码中应尽量避免死循环</span></div><div><span style="font-size: 14pt;"><br/></span></div><div><span style="font-size: 14pt;"><br/></span></div><div><span style="font-size: 21.3333px;">do...while循环</span></div><div><br clear="none"/></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><span style="font-size: 12pt; font-family: 微软雅黑;">do {</span></div><div><span style="font-size: 12pt; font-family: 微软雅黑;">    //先执行一遍代码</span></div><div><span style="font-size: 12pt; font-family: 微软雅黑;">    //while条件成立再继续反复执行</span></div><div><span style="font-size: 12pt; font-family: 微软雅黑;">} while (条件)</span></div></div><div><br clear="none"/></div><div><span style="font-size: 21.3333px;">while和do...while循环的区别</span></div><div><span style="font-size: 21.3333px;">   </span> <span style="font-size: 12pt;">do...while循环至少会执行一遍，不管条件是否成立，但是while循环如果初始条件不成立一遍也不会执行</span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="text-indent: 0pt;"><span style="font-size: 14pt; color: rgb(121, 121, 121);">    &gt;</span> <span style="font-size: 14pt; color: rgb(54, 101, 238);">打印100以内的所有奇数</span></span></div><div><span style="text-indent: 0pt;"><span style="font-size: 14pt; color: rgb(54, 101, 238);"><span style="font-size: 14pt; color: rgb(121, 121, 121);">    &gt;</span> <span style="font-size: 14pt; color: rgb(54, 101, 238);">打印一个数的所有因数</span></span></span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 16pt;">break和continue关键字</span></div><div><span style="font-size: 12pt;">    break：跳出循环体，整个循环结束</span></div><div><span style="font-size: 12pt;">    continue：结束本次循环进入下一次循环体</span></div><div><br clear="none"/></div><div><br clear="none"/></div><ul><li><div><span style="text-indent: 0pt;"><span style="font-size: 12pt; color: rgb(1, 1, 1);">   </span> <span style="font-size: 12pt; color: rgb(1, 1, 1);">入职薪水10K，每年涨幅5%，50年后工资多少？</span></span></div></li><li><div><span style="text-indent: 0pt;"><span style="font-size: 12pt; color: rgb(1, 1, 1);">   </span> <span style="font-size: 12pt; color: rgb(1, 1, 1);">逢7过游戏，打印1-100，遇到能被7整除或者带7的数字打印“过”代替</span></span></div></li><li><div><span style="font-size: 12pt;">    一张纸的厚度是0.0001米，将纸对折，对折多少次厚度超过珠峰高度8848米（27次）</span></div></li></ul><div>    </div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 21.3333px; color: rgb(1, 1, 1);">for循环</span></div><div><span style="font-size: 21.3333px; color: rgb(1, 1, 1); font-family: ΢���ź�;">    </span></div><div><span style="font-size: 21.3333px; color: rgb(1, 1, 1); font-family: ΢���ź�;">    </span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><span style="font-family: ΢���ź�; color: #010101;"><span style="font-size: 12pt;"><span style="min-height: 26pt; font-family: consolas; color: #577909;">for</span> <span style="min-height: 26pt; font-family: consolas; color: #080808;">(</span><span style="min-height: 26pt; font-family: consolas; color: #364bc0;">var</span> <span style="min-height: 26pt; font-family: consolas; color: #080808;">i</span> <span style="min-height: 26pt; font-family: consolas; color: #577909;">=</span> <span style="min-height: 26pt; font-family: consolas; color: #9b1ceb;">0</span><span style="min-height: 26pt; font-family: consolas; color: #080808;">; i</span> <span style="min-height: 26pt; font-family: consolas; color: #577909;">&lt;</span> <span style="min-height: 26pt; font-family: consolas; color: #9b1ceb;">10</span><span style="min-height: 26pt; font-family: consolas; color: #080808;">; i</span><span style="min-height: 26pt; font-family: consolas; color: #577909;">++</span><span style="min-height: 26pt; font-family: consolas; color: #080808;">) {</span></span></span></div><div><span style="font-size: 12pt;"><span style="min-height: 26pt; font-family: consolas; color: #3c7a03;">    console</span><span style="min-height: 26pt; font-family: consolas; color: #080808;">.log(i);</span></span></div><div style="min-height: 26pt;"><span style="min-height: 26pt; font-family: consolas; color: #080808;"><span style="font-size: 12pt;">}</span></span></div></div><div><br clear="none"/></div><div><span style="font-size: 21.3333px; color: rgb(1, 1, 1); font-family: ΢���ź�;"><img src="00-笔记_files/Image.png" type="image/png" data-filename="Image.png" style="font-family: ΢���ź�; font-size: 16pt; color: #010101;"/></span></div><div><br clear="none"/></div><div><span style="font-size: 21.3333px;"><span style="font-size: 21.3333px; color: rgb(227, 0, 0);">for循环1，2，4步是可选的</span></span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 16pt;">三种循环的联系和区别</span></div><div><br clear="none"/></div><div><span style="font-size: 14pt;">1、都是会反复执行的代码块</span></div><div><span style="font-size: 14pt;">2、大部分情况下可以互相替换</span></div><div><span style="font-size: 14pt;">3、do...while至少执行一次，while和for有可能0次，while不太能确定执行次数，for绝大部分情况下都可以确定</span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 21.3333px;">练习（while和for两种方式）</span></div><div><br clear="none"/></div><div><span style="font-size: 16px;">- 打印100以内所有偶数的和</span></div><div><span style="font-size: 16px;">- 求出1-1/2+1/3-1/4…..1/100的和</span></div><div><span style="font-size: 16px;">- 打印出1000-2000年中所有的闰年，并以每行四个数的形式输出</span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 16px;">-  打印三角形</span></div><div><span style="font-size: 16px; font-family: &quot;Yu Gothic UI&quot;;">                       <img src="00-笔记_files/Image [1].png" type="image/png" data-filename="Image.png" style="font-size: 12pt;"/></span></div><div><br clear="none"/></div><div><span style="font-size: 12pt;"><span style="font-size: 16px;">-  </span>输出100-200之间所有的素数（只能被1和自己整除的数）</span></div><div><br clear="none"/></div><div><span style="font-size: 12pt; color: rgb(45, 79, 201);">       for循环的嵌套 ： 可以简单的理解为行和列的关系（外层for循环控制行，内层循环控制列）</span></div><div><span style="font-size: 12pt; color: rgb(227, 0, 0);">       </span><span style="font-size: 12pt;"><span style="font-size: 12pt; color: rgb(45, 79, 201);">断点的使用，单步调试</span></span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><br/></div><ul><li><div><span style="text-indent: 0pt;"><span style="font-size: 12pt;">打印100–200之间所有能被3或者7整除的数</span></span></div></li><li><div><span style="text-indent: 0pt;"><span style="font-size: 12pt;">计算100的阶乘</span></span></div></li><li><div><font style="font-size: 12pt;"><span style="text-indent: 0pt;min-height: 19pt;font-size: 12pt;-en-paragraph:true;">求100-999之间的水仙花数</span><span style="text-indent: 0pt;min-height: 19pt;font-size: 12pt;-en-paragraph:true;">。</span></font><span style="text-indent: 0pt;min-height: 19pt;-en-paragraph:true;"><span style="font-size: 18pt;"><span style="font-size: 18pt; color: rgb(1, 1, 1); font-family: &quot;Times New Roman&quot;; font-style: italic; font-weight: bold;">abc</span></span></span> <span style="font-size: 14pt; text-indent: 0pt; min-height: 19pt; color: rgb(1, 1, 1); font-weight: bold;-en-paragraph:true;">=</span><span style="font-size: 14pt; font-weight: bold;"><img src="00-笔记_files/Image [2].png" type="image/png" data-filename="Image.png" style="color: #010101; text-indent: 0pt;" width="29"/></span><span style="font-size: 14pt; text-indent: 0pt; min-height: 19pt; color: rgb(1, 1, 1); font-weight: bold;-en-paragraph:true;">+</span><span style="font-size: 14pt; font-weight: bold;"><img src="00-笔记_files/Image [3].png" type="image/png" data-filename="Image.png" style="color: #010101; text-indent: 0pt;" width="29"/></span><span style="font-size: 14pt; text-indent: 0pt; min-height: 19pt; color: rgb(1, 1, 1); font-weight: bold;-en-paragraph:true;">+</span><span style="font-size: 14pt; font-weight: bold;"><img src="00-笔记_files/Image [4].png" type="image/png" data-filename="Image.png" style="color: #010101; text-indent: 0pt;" width="27"/></span></div></li><li><div><span style="text-indent: 0pt;"><span style="font-size: 12pt; color: rgb(1, 1, 1);">打印九九乘法表</span></span></div></li><li><div><span style="font-size: 12pt;">求1+2!+3!+...+20!的值</span></div></li></ul><div><br clear="none"/></div><div><span style="font-size: 12pt;"><br/></span></div><div><span style="font-size: 12pt;">附加题：</span></div><div><br clear="none"/></div><ul><li><div><span style="text-indent: 0pt;"><span style="font-size: 12pt; color: rgb(1, 1, 1);">完成一个三角形打印功能</span></span></div></li></ul><ul><li><div><span style="text-indent: 0pt;"><span style="font-size: 12pt; color: rgb(1, 1, 1);">完成一个梯形打印功能</span></span></div></li></ul><div><span style="color: rgb(1, 1, 1);"><span style="font-size: 12pt;">       </span> <font style="font-size: 10pt;">提示：找到行与列之间的关系，列出空格数量以及*数量跟行列之间的公式，再使用循环嵌套来打印<br/></font></span></div><div><br/></div><div><br clear="none"/></div><div><span style="text-indent: 0pt;"><span style="font-size: 12pt; color: rgb(1, 1, 1);">有一个棋盘，有64个方格，在第一个方格里面放1粒芝麻重量是0.00001kg，第二个里面放2粒，第三个里面放4，按每一个格子是前一个格子的二倍（第四个格子放8个）计算，问棋盘上放的所有芝麻的重量是多少</span></span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><div><br/></div><table style="border-collapse: collapse; min-width: 100%;" width="650"><colgroup><col style="width: 650px;"></col></colgroup><tbody><tr><td rowspan="1" style="width: 650px; padding: 8px; border: 1px solid;"><span style="font-size: 12pt;">    这是一个很著名的故事：阿基米德与国王下棋，国王输了，国王问阿基米德要什么奖赏？阿基米德对国王说：“我只要在棋盘上第一格放一粒米，第二格放二粒，第三格放四粒，第四格放八粒…按这个方法放满整个棋盘就行．”国王以为要不了多少粮食，就随口答应了，结果国王把国库粮仓所有的粮食都拿出来也放不够棋盘，只能认输了．</span></td></tr></tbody></table><div><br clear="none"/></div></div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 21.3333px;">    </span></div><div><br clear="none"/></div><div><br clear="none"/></div><div><img src="00-笔记_files/微信图片_20180725224700.png" type="image/png" data-filename="微信图片_20180725224700.png"/></div><div><br clear="none"/></div></div><div><br/></div></span>
</div></body></html> 